package com.example.demo;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JSONUtils {
    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();;
        UserInfo userInfo = new UserInfo();
        userInfo.setAge(18);
        userInfo.setUserName("zhangsan");
        userInfo.setPassword("123456");
        //将对象转为JSON字符串
        String json = objectMapper.writeValueAsString(userInfo);
        System.out.println("JSON字符串为：" + json);
        //将JSON字符串转化为对象
        UserInfo userInfo2 = objectMapper.readValue(json, UserInfo.class);
        System.out.printf("转化的对象为" + userInfo2.getUserName());
    }
}
